package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import java.time.LocalTime;
import java.time.LocalDateTime;

/**
 * 时间段实体类
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Getter
@Setter
@ToString
@EqualsAndHashCode(of = "slotId")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "time_slots", indexes = {
        @Index(name = "idx_slot_sort", columnList = "sortOrder"),
        @Index(name = "idx_slot_times", columnList = "startTime, endTime")
})
public class TimeSlotEntity {

    /**
     * 时间段ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long slotId;

    /**
     * 时间段名称
     */
    @NotBlank(message = "时间段名称不能为空")
    @Size(max = 20, message = "时间段名称长度不能超过20个字符")
    @Column(nullable = false, length = 20)
    private String slotName;

    /**
     * 开始时间
     */
    @NotNull(message = "开始时间不能为空")
    @Column(nullable = false)
    private LocalTime startTime;

    /**
     * 结束时间
     */
    @NotNull(message = "结束时间不能为空")
    @Column(nullable = false)
    private LocalTime endTime;

    /**
     * 排序顺序
     */
    @NotNull(message = "排序顺序不能为空")
    @Column(nullable = false)
    private Integer sortOrder;

    /**
     * 验证时间段是否有效
     */
    public boolean isValid() {
        return slotName != null && !slotName.trim().isEmpty() &&
                startTime != null && endTime != null &&
                sortOrder != null && startTime.isBefore(endTime);
    }

    /**
     * 获取时间段时长（分钟）
     */
    public long getDurationInMinutes() {
        return java.time.temporal.ChronoUnit.MINUTES.between(startTime, endTime);
    }

    /**
     * 检查时间是否在该时间段内
     */
    public boolean containsTime(LocalTime time) {
        return time != null &&
                !time.isBefore(startTime) &&
                time.isBefore(endTime);
    }

    /**
     * 获取时间段显示名称（带时间）
     */
    public String getDisplayName() {
        return String.format("%s (%s - %s)", slotName, startTime, endTime);
    }
}
